using System;
using System.Collections;
using UnityEngine;

public class BallMarker : MonoBehaviour
{
	public UISprite sprite;

	public UISprite sprite0;

	public UISprite secondRebound;

	public float scaleFrom = 2f;

	public float alphaFrom = 0.3f;

	public float alphaTo = 0.81f;

	private Vector3 initialScale;

	private bool initialized;

	private void Init()
	{
		if (!initialized)
		{
			if (sprite != null)
			{
				initialScale = sprite.cachedTransform.localScale;
			}
			base.gameObject.SetActive(false);
			initialized = true;
		}
	}

	private void Awake()
	{
		Init();
	}

	public void ShowOnPos(Vector3 position, Color color, float duration)
	{
		Init();
		base.transform.position = position;
		sprite.cachedTransform.position = position;
		base.gameObject.SetActive(true);
		sprite.cachedGameObject.SetActive(true);
		sprite.color = color;
		float alpha = sprite0.alpha;
		sprite0.color = color;
		sprite0.alpha = alpha;
		secondRebound.cachedGameObject.SetActive(false);
		AnimInAndOut(duration);
	}

	public void Show(PhysicsBall ball, FlightSequence.FlightPlanStage reboundStage)
	{
		Vector3 position = ball.flightSequence.landingPos + Vector3.up * 0.01f;
		float time = reboundStage.flight.apexTime * 2f;
		Vector3 vector = reboundStage.flight.PositionAt(time);
		float timeToStage = ball.flightSequence.GetTimeToStage(reboundStage);
		base.transform.position = position;
		sprite.cachedTransform.position = position;
		if (Table.IsPosOnTable(vector))
		{
			secondRebound.cachedGameObject.SetActive(true);
			secondRebound.cachedTransform.position = vector + Vector3.up * 0.01f;
		}
		else
		{
			secondRebound.cachedGameObject.SetActive(false);
		}
		base.gameObject.SetActive(true);
		AnimIn(timeToStage, null);
	}

	protected void AnimIn(float timeTillHit, Action onComplete)
	{
		StopAllCoroutines();
		StartCoroutine(DoAnimIn(timeTillHit, onComplete));
	}

	protected void AnimInAndOut(float timeTillHit)
	{
		StopAllCoroutines();
		StartCoroutine(DoAnimInAndOut(timeTillHit));
	}

	protected IEnumerator DoAnimInAndOut(float timeTillHit)
	{
		yield return StartCoroutine(FadeFromTo(sprite, alphaFrom, alphaTo, scaleFrom, 1f, timeTillHit * 0.5f));
		yield return StartCoroutine(FadeFromTo(sprite, alphaTo, 0f, 1f, 1f, timeTillHit * 0.5f));
		base.gameObject.SetActive(false);
	}

	protected IEnumerator FadeFromTo(UIWidget widget, float alpha0, float alpha1, float scale0, float scale1, float duration)
	{
		float time = 0f;
		while (time < duration)
		{
			time += Time.deltaTime;
			float normTime = time / duration;
			widget.cachedTransform.localScale = Vector3.Lerp(initialScale * scale0, initialScale * scale1, normTime);
			widget.alpha = Mathf.Lerp(alpha0, alpha1, normTime);
			yield return null;
		}
		widget.cachedTransform.localScale = initialScale * scale1;
		widget.alpha = alpha1;
	}

	protected IEnumerator DoAnimIn(float timeTillHit, Action onComplete)
	{
		float time = 0f;
		UISprite widget = sprite;
		while (time < timeTillHit)
		{
			time += Time.deltaTime;
			float normTime = time / timeTillHit;
			widget.cachedTransform.localScale = Vector3.Lerp(initialScale * scaleFrom, initialScale, normTime);
			widget.alpha = Mathf.Lerp(alphaFrom, alphaTo, normTime);
			yield return null;
		}
		if (onComplete != null)
		{
			onComplete();
		}
	}

	public void Hide()
	{
		base.gameObject.SetActive(false);
	}
}
